//
//  AccountSysVM.m
//  AccountSys
//
//  Created by guoshiqing on 2018/5/31.
//

#import "AccountSysVM.h"
#import "Url.h"
#import "UserSettings.h"

// add by gsq for test
#import "CommonHelper.h"


@implementation AccountSysVM

// 注册请求短信验证码接口
- (void)registerSmsCodeRequestWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSource:(NSString *)source
{
    // add by gsq for test
    [CommonHelper shared].beginDate = [NSDate date];

    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:source forKey:@"sSource"]; // 注册来源 phone/weixin/facebook
    
    NSString *url = RegisterSendSmsUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 修改密码请求短信验证码接口
- (void)retrievePwdSmsCodeRequestWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    
    NSString *url = RetrieveSendSmsUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 修改手机号请求短信验证码接口
- (void)modifyMobileSmsCodeRequestWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"newZoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"newPhone"];
    
    [params setObject:UserSaveInfo.areaCode forKey:@"oldZoneNum"];
    NSString *oldPhone = [UserSaveInfo.mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:oldPhone forKey:@"oldPhone"];
    
    NSString *url = ModMobileSendSmsUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 第三方注册检查手机号是否绑定第三方账号接口
- (void)checkPhoneBindForRegisterWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSource:(NSString *)source
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:source forKey:@"sSource"];
    
    NSString *url = RegisterCheckPhoneBindUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【注册】输入密码并验证的过程
- (void)checkPwdForRegisterWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode andPwd:(NSString *)password
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:smsCode forKey:@"verifyCode"];
    [params setObject:password forKey:@"password"];
    [params setObject:@"phone" forKey:@"sSource"];
    
    NSString *url = RegisterCheckPwdUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【注册】微信注册-输入密码并验证的过程
- (void)checkPwdForRegisterFromWXWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode andPwd:(NSString *)password andOpenId:(NSString *)openId andAccessToken:(NSString *)accessToken
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:smsCode forKey:@"verifyCode"];
    [params setObject:password forKey:@"password"];
    [params setObject:openId forKey:@"sOpenId"];
    [params setObject:accessToken forKey:@"sAccessToken"];
    [params setObject:@"weixin" forKey:@"sSource"];
    
    NSString *url = RegisterCheckPwdFromWXUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【注册】FaceBook注册-输入密码并验证的过程
- (void)checkPwdForRegisterFromFBWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode andPwd:(NSString *)password andOpenId:(NSString *)openId andAccessToken:(NSString *)accessToken
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:smsCode forKey:@"verifyCode"];
    [params setObject:password forKey:@"password"];
    [params setObject:openId forKey:@"sOpenId"];
    [params setObject:accessToken forKey:@"sAccessToken"];
    [params setObject:@"facebook" forKey:@"sSource"];
    
    NSString *url = RegisterCheckPwdFromFBUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【找回密码】输入密码并验证的过程
- (void)checkPwdForRetrievePwdWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode andPwd:(NSString *)password
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:smsCode forKey:@"verifyCode"];
    [params setObject:password forKey:@"password"];
    
    NSString *url = RetrieveCheckPwdUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【登录后修改密码】验证新旧密码
- (void)checkPwdForResetPwdWithOldPwd:(NSString *)oldPwd andNewPwd:(NSString *)nowPwd
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:oldPwd forKey:@"oldPassword"];
    [params setObject:nowPwd forKey:@"newPassword"];
    
    NSString *url = ModPasswordCheckPwdUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【注册】验证短信验证码是否正确
- (void)checkSmsCodeForRegisterWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode andSource:(NSString *)source
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:smsCode forKey:@"verifyCode"];
    [params setObject:source forKey:@"sSource"];
    
    NSString *url = RegisterCheckSmsUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【找回密码】验证短信验证码是否正确
- (void)checkSmsCodeForRetrievePwdWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:smsCode forKey:@"verifyCode"];
    
    NSString *url = RetrieveCheckSmsUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【修改手机号】验证短信验证码是否正确
- (void)checkSmsCodeForModifyMobileWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andSmsCode:(NSString *)smsCode andPwd:(NSString *)pwd
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"newZoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"newPhone"];
    [params setObject:smsCode forKey:@"verifyCode"];
    
    [params setObject:UserSaveInfo.areaCode forKey:@"oldZoneNum"];
    NSString *oldPhone = [UserSaveInfo.mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:oldPhone forKey:@"oldPhone"];
    [params setObject:pwd forKey:@"password"];
    
    NSString *url = ModMobileCheckSmsUrl;
    [self sendRequestWithUrl:url andParams:params];
}

//【短信快捷登录】获取用户信息
- (void)requestUserInfoForQuickLoginWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    
    NSString *url = QuickLoginGetUserInfoUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【个人中心-修改手机号】验证输入密码是否正确
- (void)checkPwdForModifyMobileWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andPwd:(NSString *)pwd
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:pwd forKey:@"password"];
    
    NSString *url = ModMobileCheckPwdUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 获取沙盒中所有登录过的用户信息
- (void)getUserCacheInfoWithMobile:(NSString *)fullMobile withReturnBlock:(void (^)(NSString *, NSString *))block {
    
    UserCacheInfoModel *userInfo = [UserSaveInfo userInfoForMobile:fullMobile];
    block(userInfo.userName, userInfo.userAavatar);
}

// 登录请求
- (void)loginRequestWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile andPwd:(NSString *)password
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    [params setObject:password forKey:@"password"];
    
    NSString *url = LoginUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 微信登录请求
- (void)WXloginRequestWithsCode:(NSString *)sCode
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:sCode forKey:@"sCode"];
    
    NSString *url = WXLoginUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// FaceBook登录请求
- (void)FBloginRequestWithUserId:(NSString *)userId andToken:(NSString *)token
{
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:userId forKey:@"id"];
    [params setObject:token forKey:@"access_token"];
    
    NSString *url = FBLoginUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【登录】免密登录/自动登录
- (void)autoLoginRequest
{
    NSString *url = AutoLoginUrl;
    [self sendRequestWithUrl:url andParams:nil];
}

// 登出请求
- (void)logoutRequestWithToken:(NSString *)token
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:token forKey:@"d-tk"];
    
    NSString *url = LogoutUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【快捷登录】获取用户信息
- (void)getUserInfoRequestWithAreaCode:(NSString *)areaCode andMobile:(NSString *)mobile
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:areaCode forKey:@"zoneNum"];
    NSString *phone = [mobile stringByReplacingOccurrencesOfString:@" " withString:@""];
    [params setObject:phone forKey:@"phone"];
    
    NSString *url = GetUserInfoForQuickLoginUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【个人信息】修改用户信息
- (void)modifyUserInfoWithKey:(nonnull NSString *)key andValue:(id)value
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:value forKey:key];
    
    NSString *url = ModUserInfoUrl;
    [self sendRequestWithUrl:url andParams:params];
}

// 【上传device token】
- (void)sendDeviceToken:(NSString *)token
{
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:token?:@"" forKey:@"sDeviceToken"];
    NSString *url = SendDeviceTokenUrl;
    [self sendRequestWithUrl:url andParams:params];
}

@end
